<template>
  <el-row justify="center" align="middle" style="height: 50vh; background-color: #f5f7fa;">
    <el-col :span="24" :md="16" :sm="20" :xs="24" style="height: 100%;">
      <div class="card" style="padding: 40px; box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.1); border-radius: 10px; background-color: #fff; height: 100%;">
        <el-form :model="data.user" label-width="100px" style="height: 100%;">
          <el-form-item label="原密码" label-position="top">
            <el-input v-model="data.user.password" show-password placeholder="请输入原密码" style="height: 50px; font-size: 16px;" />
          </el-form-item>
          <el-form-item label="新密码" label-position="top">
            <el-input v-model="data.user.newPassword" show-password placeholder="请输入新密码" style="height: 50px; font-size: 16px;" />
          </el-form-item>
          <el-form-item label="确认新密码" label-position="top">
            <el-input v-model="data.user.confirmPasword" show-password placeholder="请再次输入新密码" style="height: 50px; font-size: 16px;" />
          </el-form-item>
          <div style="text-align: center; margin-top: 30px;">
            <el-button type="primary" @click="save" style="width: 100%; padding: 15px; font-size: 18px;">保存</el-button>
          </div>
        </el-form>
      </div>
    </el-col>
  </el-row>
</template>

<script setup>
import { reactive } from "vue"
import request from "@/utils/request";
import { ElMessage } from "element-plus";
import router from "@/router";

const data = reactive({
  user: JSON.parse(localStorage.getItem('system-user') || '{}'),
})

const save = () => {
  if (data.user.newPassword !== data.user.confirmPasword) {
    ElMessage.error('确认新密码错误')
    return
  }
  if (data.user.password === data.user.confirmPasword) {
    ElMessage.error('与原密码相同')
    return
  }
  request.put('/updatePassword', data.user).then(res => {
    if (res.code === '200') {
      ElMessage.success('修改密码成功')
      localStorage.removeItem('system-user')
      router.push('/login')
    } else {
      ElMessage.error(res.msg)
    }
  })
}
</script>
